package com.sgb.kjwl.viewmodel.shopping;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.shopping.ShopInfoPublishEntity;
import com.sgb.kjwl.model.entity.shopping.ShopPriceInfo;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.ShopCompanyInfoRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopPriceDetailsAct;
import com.sgb.link.event.LeaseEvent;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.event.EventConstant;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShopPriceDetailsModel extends BaseViewModel {

    ShopPriceDetailsAct activity;
    ShopCompanyInfoRepertory repertory;

    public ShopPriceDetailsModel(ShopPriceDetailsAct activity, ShopCompanyInfoRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getCode().equals("200"))
                    activity.showAddressPop(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    //    获取商品详情信息数据加载
    public void getQuoteGoodsInfo(String note) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getProductInquiryAPI().getQuoteGoodsInfo(note).enqueue(new Callback<BaseEntity<ShopPriceInfo>>() {
            @Override
            public void onResponse(Call<BaseEntity<ShopPriceInfo>> call, Response<BaseEntity<ShopPriceInfo>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setQuoteGoodsInfo(response.body().getData());
                } else {
                    assert response.body() != null;
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ShopPriceInfo>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    //提交数据
    public void setQuotePublish(List<ShopInfoPublishEntity> shopinfopublishentity) {
        Log.e("tag提交数据", new Gson().toJson(shopinfopublishentity));
        DialogHelper.showProgressDialog(activity, null, "提交数据中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getCommonAPI().getQuotePublish(shopinfopublishentity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(activity, "提交数据成功...");
                    activity.setView();
                } else {
                    if ("1001".equals(response.body().getCode())) {
                        MToast.showToast(activity, "采购商已删除询价");
                        EventBus.getDefault().post(LeaseEvent.make().setMessageTag(EventConstant.EVENT_BAOJIA_REFRESH));
                        activity.finish();
                    } else if ("1006".equals(response.body().getCode())) {
                        MToast.showToast(activity, "采购商已取消询价");
                        EventBus.getDefault().post(LeaseEvent.make().setMessageTag(EventConstant.EVENT_BAOJIA_REFRESH));
                        activity.setView();
                    }else{
                        MToast.showToast(activity, "提交数据失败...");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "提交数据失败...");
            }
        });
    }

    public void startH5data(String note) {
        APIManager.getInstance().getLookingforAJobAPI().getUrlSetting("pensonalQuoteDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + note;
                    showUrl(url);
                } else {
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
            }
        });
    }

    //    跳转到H5界面
    public void showUrl(String url) {
        WebViewX5HostActivity.start(activity.getActivity(), url);
        activity.finish();
    }

    //需求列表详情界面
    public void showH5Url(String no) {
        APIManager.getInstance().getLookingforAJobAPI().getUrlSetting("enquiryDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (!TextUtils.isEmpty(response.body().getData().getUrl())) {
                        String url = response.body().getData().getUrl() + "?no=" + no;
                        WebViewX5HostActivity.start(activity.getActivity(), url);
                        activity.finish();
                    } else {
                        WebViewX5HostActivity.start(activity.getActivity(), "");
                        activity.finish();
                    }
                } else {
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
            }
        });
    }
}
